package com.hellogood.yaohuo.server.mgr;

import java.util.List;

import com.hellogood.yaohuo.server.DateUtil;
import com.hellogood.yaohuo.server.dao.factory.DAOFactory;
import com.hellogood.yaohuo.shared.entity.Color;
import com.hellogood.yaohuo.shared.entity.Log;
import com.hellogood.yaohuo.shared.vo.MainFrame;

public class ColorMgr {
	public List<Color> getAllColor() {
		return DAOFactory.getInstance().createColorDAO().getAllColor();
	}

	public String addNewColor(Color c) {
		String result = "";
		if (c.getColorCode().equals("0")) {
			result = "不能创建\"0\"为编码的颜色";
		} else {
			Color temp = DAOFactory.getInstance().createColorDAO().getColorByCode(c.getColorCode());
			if (temp == null) {
				result = DAOFactory.getInstance().createColorDAO().addNewColor(c);
				if (result.equals("")) {
					int logID = DAOFactory.getInstance().createLogDAO().getLastLogNo();
					logID++;
					Log log = new Log();
					log.setLogId(logID);
					log.setStaffCode(MainFrame.currentUser.getStaffCode());
					log.setStaffName(MainFrame.currentUser.getStaffName());
					log.setLogDate(DateUtil.currentTime());
					log.setContent("新建颜色：" + c.getColorCode() + "-" + c.getColorName());
					DAOFactory.getInstance().createLogDAO().addNewLog(log);
				}
			} else {
				result = "编码：" + c.getColorCode() + " 已经存在!";
			}
		}
		return result;
	}

	public String removeColor(Color color) {
		String result = "";
		if (color.getColorCode().equals("0")) {
			result = "不能删除\"0-均色\"";
		} else {
			result = DAOFactory.getInstance().createColorDAO().removeColor(color.getColorCode());
			if (result.equals("")) {
				int logID = DAOFactory.getInstance().createLogDAO().getLastLogNo();
				logID++;
				Log log = new Log();
				log.setLogId(logID);
				log.setStaffCode(MainFrame.currentUser.getStaffCode());
				log.setStaffName(MainFrame.currentUser.getStaffName());
				log.setLogDate(DateUtil.currentTime());
				log.setContent("删除颜色：" + color.getColorCode() + "-" + color.getColorName());
				DAOFactory.getInstance().createLogDAO().addNewLog(log);
			}
		}
		return result;
	}
}
